package com.rockville.ufone;

import com.rockville.ufone.rssdata.HoroscopeParser;
import com.rockville.ufone.rssdata.RSSDataParser;
import com.rockville.ufone.rssdata.ReSendWapPush;
import com.rockville.ufone.rssdata.SimToolKitDataUpdater;
import com.rockville.ufone.rssdata.WeatherParser;

import java.net.URL;
import java.util.Iterator;
import java.util.Vector;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.apache.log4j.helpers.Loader;
import org.apache.log4j.xml.DOMConfigurator;

/**
 * 
 * @author Ahsan
 * @modified Asad, Usman, Qaiser Suleman
 */
public class ScheduledJobsHandler {

	private static final Logger log = Logger.getLogger(ScheduledJobsHandler.class);
	private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4);
	private final ScheduledExecutorService criScheduler = Executors.newScheduledThreadPool(1);
	private final ScheduledExecutorService feedSchedular = Executors.newScheduledThreadPool(2);

	private void start() {
		log.info("*** Job Schedular Started ***");
		try {
            /********************** ReSend WAP PUSH JOB **********************/
			long interval_resend_wappush = 1;
			try {
				interval_resend_wappush = Long.parseLong(SystemProperty.getProperty("interval_resend_wappush"));
			} catch (Exception exParseLong) {
				interval_resend_wappush = 60;
			}
			if(interval_resend_wappush > 0)
				scheduler.scheduleAtFixedRate(new ReSendWapPush(), 0, interval_resend_wappush, TimeUnit.MINUTES);
			
			/********************** SIM ToolKit data parser **********************/
			long interval_simtoolkit = 1;
			try {
				interval_simtoolkit = Long.parseLong(SystemProperty.getProperty("interval_simtoolkit"));
			} catch (Exception exParseLong) {
				interval_simtoolkit = 60;
			}
			if(interval_simtoolkit > 0)
				scheduler.scheduleAtFixedRate(new SimToolKitDataUpdater(), 0, interval_simtoolkit, TimeUnit.MINUTES);
			
			/********************** Weather data parser **********************/	
			long interval_weatherupdate = 1;
			try {
				interval_weatherupdate = Long.parseLong(SystemProperty.getProperty("interval_weatherupdate"));
			} catch (Exception exParseLong) {
				interval_weatherupdate = 60;
			}
			if(interval_weatherupdate > 0)
				scheduler.scheduleAtFixedRate(new WeatherParser(), 0, interval_weatherupdate, TimeUnit.MINUTES);
			
			/********************** Horoscope parser **********************/
			long interval_horoupdate = 12;
			try {
				interval_horoupdate = Long.parseLong(SystemProperty.getProperty("interval_horoupdate"));
			} catch (Exception exParseLong) {
				interval_horoupdate = 12;
			}
			if(interval_horoupdate > 0)
				scheduler.scheduleAtFixedRate(new HoroscopeParser(), 0, interval_horoupdate, TimeUnit.HOURS);
			
			/********************** Stock Market parser **********************/
//			long interval_stockupdate = 60;
//			try {
//				interval_stockupdate = Long.parseLong(SystemProperty.getProperty("interval_stockupdate"));
//			} catch (Exception exParseLong) {
//				interval_stockupdate = 60;
//			}
//			if(interval_stockupdate > 0)
//				scheduler.scheduleAtFixedRate(new StockMarketParser(), 0, interval_stockupdate, TimeUnit.MINUTES);

			/********************** CricLiveScore parser **********************/
			long interval_livescore_update = 1;
			try {
				interval_livescore_update = Long.parseLong(SystemProperty.getProperty("interval_criclivescore"));
			} catch (Exception exParseLong) {
				interval_livescore_update = 1;
			}
			if(interval_livescore_update > 0) {
				criScheduler.scheduleAtFixedRate(new CricLiveScoreParser(), 0, interval_livescore_update, TimeUnit.SECONDS);
				criScheduler.scheduleAtFixedRate(new CricLiveScoreHandler(), 0, interval_livescore_update, TimeUnit.SECONDS);
			}

			/********************** RSS Channels parser **********************/
			DBProc db = DBProc.getInstance();			
			Vector<String[]> vec = db.fetchAllFeeds();
			for (Iterator<String[]> it = vec.iterator(); it.hasNext();) {
				String[] feedArr = it.next();
				long interval_rssupdate = 1;
				try {
					interval_rssupdate = Long.parseLong(feedArr[3]);
				} catch (Exception exParseLong) {
					interval_rssupdate = 30;
				}				
				String feedUrl = feedArr[2];
				feedSchedular.scheduleAtFixedRate(new RSSDataParser(feedArr[0], feedArr[1], feedUrl), 0, interval_rssupdate, TimeUnit.MINUTES);					
			}
		} catch (Exception ex) {
			log.error("", ex);
		}
	}

	public static void main(String[] args) {
		try {
			URL url = Loader.getResource("log4j-config.xml");
			DOMConfigurator.configure(url);
			ScheduledJobsHandler rssdm = new ScheduledJobsHandler();
			rssdm.start();
		} catch (Exception e) {
			log.error("", e);
		}
	}
}
